package com.sixth.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.sixth.dao.GoodsDao;
import com.sixth.dao.OutlibraryDao;
import com.sixth.entity.Goods;
import com.sixth.entity.Outlibrary;
import com.sixth.entity.OutlibraryGoods;
import com.sixth.entity.Staff;
import com.sixth.service.OutLibraryService;
import com.sixth.util.CommonResult;
import com.sixth.util.LayTab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @program: project01
 * @description:
 * @author: 老范
 * @create: 2021-12-17 20:10
 **/
@Service(value = "outLibraryService")
public class OutLibraryServiceImpl implements OutLibraryService {
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private OutlibraryDao outlibraryDao;
    @Autowired
    private HttpSession session;
    //批量出库
    public CommonResult batch(String batchGoods) {


        List<Goods> goods = JSONArray.parseArray(batchGoods, Goods.class);

        Staff staff = (Staff) session.getAttribute("staff");
        Integer staffid = staff.getStaffid();

        Outlibrary outLibrary = new Outlibrary();
        outLibrary.setOutstaffid(staffid);
        outLibrary.setOuttime(new Date());
        outLibrary.setBranchid(goods.get(0).getBranchid());

        Integer i = outlibraryDao.addBatchOutLibrary(outLibrary);
        Integer cid = outLibrary.getCid();

        if (i>0){
            for (Goods good : goods) {
                OutlibraryGoods olg = new OutlibraryGoods();
                Integer goodsid = good.getGoodsid();
                Integer chuKuNum = good.getChuKuNum();
                Double sprice = good.getSprice();
                olg.setCid(cid);
                olg.setGoodsid(goodsid);
                olg.setNum(chuKuNum);
                olg.setTotal(chuKuNum * sprice);
                Integer integer = outlibraryDao.addOutLibraryGoods(olg);
                if (integer == 0) {
                    return CommonResult.fail("操作失败！！");
                }
            }
            return CommonResult.success(null, "操作成功！");
        }
        return CommonResult.fail("操作失败！！");
    }

    //平调出库
    public CommonResult transferOutLibrary(String batchGoods, Integer branchid) {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer staffid = staff.getStaffid();

        List<Goods> goods = JSONArray.parseArray(batchGoods, Goods.class);

        Outlibrary outLibrary = new Outlibrary();

        outLibrary.setOutstaffid(staffid);
        outLibrary.setOuttime(new Date());
        outLibrary.setNewbranchid(branchid);
        outLibrary.setBranchid(goods.get(0).getBranchid());

        Integer i = outlibraryDao.addTransferOutLibrary(outLibrary,branchid);

        Integer cid = outLibrary.getCid();

        if (i>0){
            for (Goods good : goods) {
                OutlibraryGoods olg = new OutlibraryGoods();
                Integer goodsid = good.getGoodsid();
                Integer chuKuNum = good.getChuKuNum();
                Double sprice = good.getSprice();

                olg.setCid(cid);
                olg.setGoodsid(goodsid);
                olg.setNum(chuKuNum);
                olg.setTotal(chuKuNum * sprice);

                Integer integer = outlibraryDao.addOutLibraryTransferGoods(olg);
                if (integer == 0) {
                    return CommonResult.fail("操作失败！！");
                }
            }
            return CommonResult.success(null, "操作成功！");
        }
        return CommonResult.fail("操作失败！！");
    }



    //零售出库
    public CommonResult retailOutLibrary(Integer branchid, Integer goodsid, Integer counts,Integer chuKuNum) {

        outlibraryDao.retailOutLibrary(branchid, goodsid, counts,chuKuNum);
        Staff staff = (Staff) session.getAttribute("staff");

        Integer staffid = staff.getStaffid();
        //把出库申请人填进去
        Outlibrary outLibrary = new Outlibrary();
        outLibrary.setOutstaffid(staffid);
        outLibrary.setOuttime(new Date());
        outLibrary.setBranchid(branchid);

        outlibraryDao.addOutLibrary(outLibrary);
        Integer cid = outLibrary.getCid();

        Goods goodsPrice = goodsDao.getGoodsPrice(goodsid);
        Double sprice = goodsPrice.getSprice();

        OutlibraryGoods olg = new OutlibraryGoods();

        olg.setCid(cid);
        olg.setGoodsid(goodsid);
        olg.setNum(chuKuNum);
        olg.setTotal(chuKuNum*sprice);
        Integer integer = outlibraryDao.addOutLibraryGoods(olg);

        CommonResult result;
        if (integer > 0) {
            result = CommonResult.success(null, "操作成功");
        } else {
            result = CommonResult.fail("操作失败");
        }
        return result;
    }

    public LayTab outLibraryDetails(Integer cid) {
        List<Map> maps = outlibraryDao.outLibraryDetails(cid);
        LayTab layTab = new LayTab(maps.size(), maps);
        return layTab;
    }

    //平调出库详情
    public LayTab outLibraryTransferDetails(Integer cid) {
        List<Map> maps = outlibraryDao.outLibraryTransferDetails(cid);
        LayTab layTab = new LayTab(maps.size(), maps);
        return layTab;
    }
}
